package com.nk.iexam;

import java.util.List;

import com.nk.iexam.adapter.SubjectAdapter;
import com.nk.iexam.ds.SubjectDataSource;
import com.nk.iexam.models.Subject;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity
{
	private List<Subject> _subjects;
	private RadioButton _btnReview;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		SubjectDataSource  subjectDataSource = new SubjectDataSource();
		ListView listView = (ListView) findViewById(R.id.subjectListView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
				_btnReview = (RadioButton) findViewById(R.id.radioReview);
				Subject subject = _subjects.get(position - 1);

				Intent intent = null;
				if (_btnReview.isChecked()) {
					intent = new Intent(MainActivity.this, SubjectActivity.class);
				}
				else {
					intent = new Intent(MainActivity.this, MultichoicesActivity.class);
				}

				intent.putExtra(SubjectActivity.SUBJECT, subject);
				startActivity(intent);
			}
		});
		View header = (View)getLayoutInflater().inflate(R.layout.list_header, null);
		listView.addHeaderView(header);

		_subjects = subjectDataSource.getAllDbObjects();
		listView.setAdapter(new SubjectAdapter(this, R.layout.list_row, _subjects));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}    
}
